package com.youlai.warehouse.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.system.model.form.RoleForm;
import com.youlai.system.model.form.UserForm;
import com.youlai.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.youlai.warehouse.model.form.BasicProductForm;
import com.youlai.warehouse.model.query.BasicProductPageQuery;
import com.youlai.warehouse.model.vo.BasicProductPageVo;
import com.youlai.warehouse.service.BasicProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "01.基础信息")
@RestController
@RequestMapping("/api/v1/basic/product")
@RequiredArgsConstructor
public class BasicProductController {

    @Autowired
    private BasicProductService basicProductService;

    @Operation(summary = "产品信息列表")
    @GetMapping("/page")
    public PageResult<BasicProductPageVo> getBasicProductPage(
            @ParameterObject BasicProductPageQuery queryParams
    ) {
        IPage<BasicProductPageVo> result = basicProductService.getBasicProductPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增产品信息")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:basicProduct:add')")
    @PreventDuplicateSubmit
    public Result saveBasicProduct(
            @RequestBody @Valid BasicProductForm basicProductForm
    ) {
        boolean result = basicProductService.saveBasicProduct(basicProductForm);
        return Result.judge(result);
    }

    @Operation(summary = "产品信息表单数据")
    @GetMapping("/{id}/form")
    public Result<BasicProductForm> getBasicProductForm(
            @Parameter(description = "产品信息ID") @PathVariable Long id
    ) {
        BasicProductForm basicProductForm = basicProductService.getbasicProductForm(id);
        return Result.success(basicProductForm);
    }

    @Operation(summary = "修改产品信息")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('sys:basicProduct:edit')")
    public Result updateBasicProduct(@Valid @RequestBody BasicProductForm basicProductForm) {
        boolean result = basicProductService.updateBasicProduct(basicProductForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除产品信息")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:basicProduct:delete')")
    public Result deleteBasicProduct(
            @Parameter(description = "删除角色，多个以英文逗号(,)拼接") @PathVariable String ids
    ) {
        boolean result = basicProductService.deleteBasicProduct(ids);
        return Result.judge(result);
    }

}
